# -*- coding: utf-8 -*-

import numpy as np
import pandas as pd
import tensorflow as tf

#预处理特征

#创建用户点击与安装记录

def create_AppActions():
    #读入数据
    print ('读入 user_app_actions.csv')
    user_app_actions=pd.read_csv('data/origin/user_app_actions.csv')
    print ('读入完成/n读入app_categories.csv')
    app_categories=pd.read_csv('data/origin/app_categories.csv')
    print ('读入完成')

    print ('数据合并')
    user_app_categories=pd.merge(user_app_actions,app_categories,how='left',on='appID')
    print ('合并完成')

    #数据总数
    total_num=len(user_app_actions)

    #统计用户最近时间安装数量，安装类别
     #获取userID
    userID_list=user_app_actions['userID'].values
    userID_list=list(set(userID_list))
     #安装时间
    dict_time={}
     #安装app
    dict_app={}
     #安装类别
    dict_categories={}
     #初始化字典
    for user in userID_list:
        dict_time[user]=''
        dict_app[user]=''
        dict_categories[user]=''
    print('开始录入字典')

    count=0
    for i,j,k,z in user_app_categories.values:
        dict_time[i]+=(str(j)+' ')
        dict_app[i]+=(str(k)+' ')
        #统计type
        ca=str(z)
        if ca=='':
            ca='0'
        dict_categories[i]+=(ca+' ')
        count+=1
        if count%100000==0:
            print('字典已加载: %.2f %%'%(count/total_num*100))
    print('统计完成，开始保存')

    #保存字典
    userID_list=list(dict_time.keys())
    app_time=list(dict_time.values())
    app_action=list(dict_app.values())
    app_type=list(dict_categories.values())
    
    df_list=pd.DataFrame({'userID':userID_list,
                          'timeList':app_time,
                          'actionList':app_action,
                          'action_type':app_type}) 
    df_list.to_csv('data/feature/action_list_v1.csv',index=False)
    
if __name__=='__main__':
    create_AppActions()






